#!/usr/bin/env perl
package BugzillaJsonRpc::Bug;
#https://www.bugzilla.org/docs/tip/en/html/api/Bugzilla/WebService/Bug.html#create
use 5.018;
use warnings;
use utf8;
use BugzillaJsonRpc::Client;
use Data::Dumper;
use Mojo::JSON qw(true false);

sub attachments
{
	my ($bug_id,$cb) = @_;
	my $params = { ids => [ $bug_id ] };
	BugzillaJsonRpc::Client::call("Bug.attachments", $params, sub{
		my ($ret) = @_;
		unless ($ret->{error})
		{
			$ret->{result} = $ret->{result}{bugs}{ $bug_id };
		}
		warn 'Bug.attachments Dumper $ret';
		warn Dumper $ret;
		$cb->($ret);
	});
}


sub get
{
	my ($bug_id, $cb) = @_;
	my $params = { ids => [ $bug_id ] };
	BugzillaJsonRpc::Client::call("Bug.get", $params, sub {
		my ($ret) = @_;
		unless ($ret->{error})
		{
			$ret->{result} = $ret->{result}{bugs}[0];
		}
		warn 'Bug.get Dumper $ret';
		warn Dumper $ret;
		$cb->($ret);
	});
}

# params key:
# product (string) required
# component (string) required
# summary (string) required
# version (string) required
# description (string) defaulted
# op_sys (string) defaulted
# platform (string) defaulted
# priority (string) defaulted
# severity (string) defaulted
#

sub create
{
	my ($params, $cb) = @_;
	my $result = BugzillaJsonRpc::Client::call("Bug.create", $params, sub {
		my ($ret) = @_;
		unless ($ret->{error})
		{
			$ret->{result} = $ret->{result};
		}
		warn 'Bug.create Dumper $ret';
		warn Dumper $ret;
		$cb->($ret);
			
	});
}

# params keys:
# file_name (string) required
# summary (string) defaulted
use MIME::Base64 qw(encode_base64);
sub AddAttachment
{
	my ($bug_id, $params, $cb) = @_;
	$params->{ids} = [ $bug_id ];
	my $file_name = $params->{file_name};

	#set summary default to file_name
	$params->{summary} ||= $file_name,

	#file base64 encode
	open DATA, '<', $file_name;
	my $data = do {
		#read whole file
		local $/;
		encode_base64(<DATA>);
	};
	close DATA;
	$params->{data} = $data;

	BugzillaJsonRpc::Client::call("Bug.add_attachment", $params, sub {
		my ($ret) = @_;
		unless ($ret->{error})
		{
			$ret->{result} = $ret->{result}{ids}[0];
		}
		warn 'Bug.AddAttachment Dumper $ret';
		warn Dumper $ret;
		$cb->($ret);
		
	});
}


# param keys
# comment string required
# is_private boolean
# is_markdonw boolean
# work_time double
# minor_update boolean
sub addComment
{
	my ($bug_id, $params, $cb) = @_;
	$params->{id} = $bug_id;
	BugzillaJsonRpc::Client::call("Bug.add_comment", $params, sub {
		my ($ret) = @_;
		unless ($ret->{error})
		{
			$ret->{result} = $ret->{result}{id};
		}
		warn 'Bug.addComment Dumper $ret';
		warn Dumper $ret;
		$cb->($ret);
	});

}

sub update
{
	my ($bug_id, $params, $cb) = @_;
	if ( ref $bug_id eq 'ARRAY' )
	{
		$params->{ids} = $bug_id; #bug ids
	}
	else
	{
		$params->{ids} = [ $bug_id ];
	}

	BugzillaJsonRpc::Client::call("Bug.update", $params, sub {
		my ($ret) = @_;
		unless($ret->{error})
		{
			$ret->{result} = $ret->{result}{bugs}[0];
		}
		warn 'Bug.update dump $ret';
		warn Dumper $ret;
		$cb->($ret);
	});
}

sub updateSeeAlso
{
	my ($bug_id, $params, $cb) = @_;
	$params->{ids} = [ $bug_id ];
	BugzillaJsonRpc::Client::call("Bug.update_see_also", $params, sub {
		my ($ret) = @_;
		$cb->($ret);
	});
}

1;
